/**
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *	
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.brightcove.android.oneplanet.view;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Super class for the VideoView and PlaylistView classes.
 */
public class CellView extends LinearLayout {
	private static final String TAG = "CellView";

	protected ImageView cellIcon;
	protected TextView cellLabel;

	public CellView(Context context) {
		super(context);
		this.setPadding(3, 3, 3, 3);
		
		cellIcon = new ImageView(context);
		cellIcon.setAdjustViewBounds(true);
		cellIcon.setMaxHeight(65);
		addView(cellIcon, 
				new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.7f));
		
		cellLabel = new TextView(context);
		cellLabel.setTextSize(24);
		cellLabel.setPadding(12, 10, 6, 6);
		addView(cellLabel, 
				new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.3f));
	}

	public CellView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * Returns images to be used in the ListViews of the application.
	 */
	protected Bitmap getImageBitmap(String url) { 
	    Bitmap bm = null; 
	    try { 
	        URL aURL = new URL(url); 
	        URLConnection conn = aURL.openConnection(); 
	        conn.connect(); 
	        InputStream is = conn.getInputStream(); 
	        BufferedInputStream bis = new BufferedInputStream(is); 
	        bm = BitmapFactory.decodeStream(bis); 
	        bis.close(); 
	        is.close(); 
	   } catch (IOException e) { 
	       Log.e(TAG, "Error getting bitmap", e); 
	   } 
	   return Bitmap.createScaledBitmap(bm, 110, 65, true); 
	}

}